<?php
namespace app\index\controller;
use app\common\model;
use app\common\service\BaiduApiService;
class Meeting extends Base {

    protected $apiKey = 'XsGv3ESddbypqiLDkYzbrPNz';
    public $client_id='11613254';
    public $client_secret='HriDWHhhNp78H3NlWOaO5TGe2G66zWlt';
    public $meetingModel;
    public $private_key = '@fdwqe!231~';
    public function __construct() {
        parent::__construct();
        $this->meetingModel = new model\Meeting();
        $this->signModel = new model\Sign();
    }
    public function index() {
        // 
        $token = $_GET['token'];
        $meeting = $this->meetingModel->getOne(['token'=>$token]);
        if(!empty($meeting)) {
            $brief = $meeting['desc'];
            $start_time = $meeting['start_time'];
            $start_time = date('Y-m-d', strtotime($meeting['start_time']));
            $end_time = date('Y-m-d', strtotime($meeting['end_time']));
            $start_time_arr = explode('-',$start_time);
            $start_time = $start_time_arr[1].'月'.$start_time_arr[2].'日';
            $end_time_arr =  explode('-',$end_time);
            $end_time = $end_time_arr[1].'月'.$end_time_arr[2].'日';
            $meetingname = $meeting['meeting_name'];
            $logo = $meeting['headimage'];
        }
       
        $bgimage = 'http://wsimg.waoo.cc/upload/goods/2018-07/30/511431532925631.jpg';
        //$meetingname = '第三届舞者大会';
        $meetingInfo = array(
            'brief'=>$brief,
            'bgimage'=>$bgimage,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'meetingname'=>$meetingname,
            'logo'=>$logo,
            'token'=>$token
        );
        $this->assign("meetingInfo", $meetingInfo);//不需要购物车悬浮窗
        return view();
    }
    public function form() {
        return view();
    }
    public function over($key='') {
        $access_key = $key?$key:$_GET['access_key'];
        //解密
        $id = intval(xn_decrypt($access_key, $this->private_key));
        $sign = $this->signModel->getOne(['id'=>$id]);
        if(empty($sign)) {
            //$this->ajax(202, 'error', '报名失败');
        }
        $meetingid = $sign['meeting_id'];
        $qrcode_url = $sign['qrcode_url'];
        $meeting = $this->meetingModel->getOne(['id'=>$meetingid]);
        if(!empty($meeting)) {
            $brief = $meeting['desc'];
            $start_time = $meeting['start_time'];
            $start_time = date('Y-m-d', strtotime($meeting['start_time']));
            $end_time = date('Y-m-d', strtotime($meeting['end_time']));
            $start_time_arr = explode('-',$start_time);
            $start_time = $start_time_arr[1].'月'.$start_time_arr[2].'日';
            $end_time_arr =  explode('-',$end_time);
            $end_time = $end_time_arr[1].'月'.$end_time_arr[2].'日';
            $meetingname = $meeting['meeting_name'];
            $logo = $meeting['headimage'];
        }
       
        $bgimage = 'http://wsimg.waoo.cc/upload/goods/2018-07/30/511431532925631.jpg';
        //$meetingname = '第三届舞者大会';
        $meetingInfo = array(
            'brief'=>$brief,
            'bgimage'=>$bgimage,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'meetingname'=>$meetingname,
            'logo'=>$logo,
            'token'=>$token
        );
        $this->assign("meetingInfo", $meetingInfo);//不需要购物车悬浮窗
        return view();
    }

    public function uploadimg() {
        $data = $_POST;
        if(isset($_POST['card_file']) || isset($_POST['avatar_file'])) {
            $token = BaiduApiService::S()->auth();
            if(isset($_POST['card_file'])) {
                $data = $_POST['card_file'];
                $data = explode(',', $data);
                //$data = urlencode($data[1]);
                //请求api模拟返回数据->正确上传七牛云并返回链接。->失败返回错误信息。
                //print_r($token);exit;
                $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token='.$token;
                $bodys = array(
                    'image' => $data[1],
                    'id_card_side'=>'front'
                );
                //$bodys = json_encode($bodys);
                $res = request_post($url, $bodys);
                $res_arr = json_decode($res, true);
               
               // print_r($res_arr);exit;
            
                if(isset($res_arr['words_result'])) {
                    $words_res = $res_arr['words_result'];
                    // idcard  name
                    $idcard = $words_res['公民身份号码']['words'];
                    $name = $words_res['姓名']['words'];
                    $userinfo = array(
                        'idcard'=>$idcard,
                        'name'=>$name
                    );
                    return  ajaxReturn(0, 'ok', $userinfo);
                } else if($res_arr['error_code'] == 110){
                    $token = BaiduApiService::S()->auth(true);
                    if(empty($token)) {
                        return   ajaxReturn(1, 'error', '网络繁忙，请稍后再试');
                    }
                    $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token='.$token;
                    $res = request_post($url, $bodys);
                    $res_arr = json_decode($res, true);
                   
                    // print_r($res_arr);exit;
                    if(isset($res_arr['words_result'])) {
                        $words_res = $res_arr['words_result'];
                        // idcard  name
                        $idcard = $words_res['公民身份号码']['words'];
                        $name = $words_res['姓名']['words'];
                        $userinfo = array(
                            'idcard'=>$idcard,
                            'name'=>$name
                        );
                        return  ajaxReturn(0, 'ok', $userinfo);
                    }
                } else {
                    return   ajaxReturn(1, 'error', '未检测出数据，请重新上传');
                }
              
                
            }
            if(isset($_POST['avatar_file'])) {
                $data = $_POST['avatar_file'];
                $data = explode(',', $data);
                //$data = urlencode($data[1]);
                //请求api模拟返回数据->正确上传七牛云并返回链接。->失败返回错误信息。
                $url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token='.$token;
                $bodys = array(
                    'image'=>$data[1],
                    'image_type'=>'BASE64',
                    //'face_type'=>'CERT'
                );
                $bodys = json_encode($bodys);
                $res = request_post($url, $bodys);
                $res_arr = json_decode($res, true);
                if(isset($res_arr['error_msg']) && $res_arr['error_msg'] == 'SUCCESS') {
                    
                    if($res_arr['result']['face_list'][0]['face_probability'] < 0.9) {
                        return ajaxReturn(1, 'error', '人脸照片不完整，请重新截取');
                    }
                    
                    return ajaxReturn(0, 'ok', '检测成功');
                } else {
                   
                    return ajaxReturn(1, 'error', '检测失败');
                }
                
                
            } 
        }
    }

}
